import unittest
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class A3mall(unittest.TestCase):
    def setUp(self) -> None:
        print('start case')

    def tearDown(self) -> None:
        print('end case')

    order_no = None

    def test_01_qian(self):
        driver = webdriver.Edge()
        driver.get('http://10.59.9.12:7001/')
        sleep(2)
        driver.maximize_window()
        sleep(2)
        # 点击请登录
        dl = WebDriverWait(driver, 10, 0.5).until(
            EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a')))
        dl.click()
        # driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
        sleep(2)
        # 输入账号
        driver.find_element(By.NAME, 'username').send_keys('18185607777')
        sleep(2)
        # 输入密码
        driver.find_element(By.NAME, 'password').send_keys('123456')
        sleep(10)
        # 点击登录
        driver.find_element(By.CLASS_NAME, 'finish-btn').click()
        sleep(5)
        # 提取登录时的用户余额
        login_yue = driver.find_element(By.CLASS_NAME, 'color-black').text
        sleep(2)
        login_yueS = login_yue.split()
        print('登录时的用户余额:', login_yueS[1])
        # 点击首页
        driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/ul/li[1]/a').click()
        sleep(2)
        # 向下滑动
        driver.execute_script('window.scrollTo(0,500)')
        sleep(2)
        # 点击商品
        driver.find_element(By.XPATH, '/html/body/div[8]/div[1]/div[2]/div[2]/ul/li[3]/div[1]/a/img').click()
        sleep(2)
        # 点击立即购买
        driver.find_element(By.ID, 'buy-cart').click()
        sleep(2)
        # 向下滚动
        driver.execute_script("window.scrollTo(0,500)")
        sleep(2)
        # 提取商品需要支付的总金额（包含运费、数量、单价）
        order_amount = driver.find_element(By.ID, 'order-amount').text
        print('订单总价为：', order_amount)
        sleep(2)
        # 点击下一步
        driver.find_element(By.ID, 'order-button').click()
        sleep(2)
        # 点击查看订单
        driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/p/a').click()
        sleep(2)
        # 提取购买商品的订单编号
        self.order_no = driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[2]').text
        print('前端-商品订单号', self.order_no)
        # 点击申请退款
        driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[6]/td[2]/div[1]/a').click()
        sleep(2)
        # 输入退款说明
        driver.find_element(By.NAME, 'message').send_keys('颜色买错了')
        sleep(2)
        # 点击提交
        driver.find_element(By.ID, 'user-info-btn').click()
        sleep(5)
        # 点击会员中心
        driver.find_element(By.XPATH, '/html/body/div[4]/a[2]').click()
        sleep(2)
        # 用登录余额 - 购买商品的总支付金额 = 用户此时余额
        gmh_yue = float(login_yueS[-1]) - float(order_amount)
        print('此时购买商品后的余额：', gmh_yue)
        sleep(2)
        # 提取购买商品后 会员中心的总余额
        yue = driver.find_element(By.CLASS_NAME, 'color-black').text
        sleep(2)
        # 提取出来后 进行切割取金额
        yueS = str(yue).split(' ')
        print(str(yueS[1]))
        sleep(2)
        # 断言 用户购买后的商品余额 和 此时的会员中心余额 是否一致
        self.assertEqual(float(yueS[1]), float(gmh_yue))
        # 关闭浏览器驱动
        sleep(3)
        # driver.quit()

        # A3mall-后台
        driver = webdriver.Edge()
        driver.get('http://10.59.9.12:7001/admin/')
        driver.maximize_window()
        sleep(2)
        # 输入账号
        driver.find_element(By.NAME, 'username').send_keys('admin')
        sleep(2)
        # 输入密码
        driver.find_element(By.NAME, 'password').send_keys('admin888')
        sleep(10)
        # 点击登录
        driver.find_element(By.XPATH, '/html/body/div[2]/div/div/form/div[4]/button').click()
        sleep(5)
        # 点击查看 退款订单
        driver.find_element(By.XPATH, '/html/body/div/div/section/div/div[2]/div[2]/div[3]/div/div/div/a').click()
        sleep(7)
        # 查找 对应的订单编号
        # a = self.order_no
        # print(a)
        # bianhao = driver.find_element(By.LINK_TEXT, str(a)).text
        # print('后端-商品订单号', bianhao)
        # 点击详情
        # 点击退款
        driver.find_element(By.XPATH, '/html/body/div/div/section/div/form/div/div/div[1]/div[1]/div/a[3]').click()
        sleep(2)
        # 向下滑动
        driver.execute_script('window.scrollTo(0,300)')
        sleep(5)
        # 输入内容
        driver.find_element(By.NAME, 'desc').send_keys('同意退款该商品')
        sleep(2)
        # 点击提交
        driver.find_element(By.XPATH, '/html/body/div/div/section/div/form/div/div[2]/div/button[1]').click()
        sleep(2)
        # 关闭浏览器驱动
        sleep(3)
        driver.quit()

        # A3mall-前台
        # driver = webdriver.Edge()
        driver.get('http://10.59.9.12:7001/')
        sleep(2)
        driver.maximize_window()
        sleep(2)
        # 点击请登录
        dl = WebDriverWait(driver, 10, 0.5).until(
            EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a')))
        dl.click()
        # driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
        sleep(2)
        # 输入账号
        driver.find_element(By.NAME, 'username').send_keys('18185607777')
        sleep(2)
        # 输入密码
        driver.find_element(By.NAME, 'password').send_keys('123456')
        sleep(10)
        # 点击登录
        driver.find_element(By.CLASS_NAME, 'finish-btn').click()
        sleep(5)
        # 提取退款后的用户余额
        tk_yue = driver.find_element(By.CLASS_NAME, 'color-black').text
        sleep(2)
        tk_yueS = login_yue.split()
        print('退款后的用户余额:', tk_yueS[1])
        sleep(2)
        # 点击我的订单
        driver.find_element(By.XPATH, '/html/body/div[5]/div[1]/div[2]/div[1]/div[2]/ul/li[1]/a').click()
        sleep(2)
        # 提取后台退款的总金额
        tk_money = driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[2]/table/tbody/tr[3]/td[2]').text
        tk_moneyS = tk_money.split('￥')
        print('后台退款的总金额:', tk_moneyS[1])
        sleep(2)
        # 用登录时的余额  退款的总余额 = 用户退款后的余额
        user_syye = float(yueS[1]) + float(tk_moneyS[1])
        print('退款后的用户余额应该为：', user_syye)
        sleep(2)
        # 计算的退款后余额 和 用户页面显示的余额 是否相等
        self.assertEqual(float(user_syye), float(tk_yueS[1]))
        # 关闭浏览器驱动
        sleep(3)
        driver.quit()

    @unittest.skip
    def test_02_hou(self):
        driver = webdriver.Edge()
        driver.get('http://10.59.9.12:7001/admin/')
        driver.maximize_window()
        sleep(2)
        # 输入账号
        driver.find_element(By.NAME, 'username').send_keys('admin')
        sleep(2)
        # 输入密码
        driver.find_element(By.NAME, 'password').send_keys('admin888')
        sleep(10)
        # 点击登录
        driver.find_element(By.XPATH, '/html/body/div[2]/div/div/form/div[4]/button').click()
        sleep(5)
        # 点击查看 退款订单
        driver.find_element(By.XPATH, '/html/body/div/div/section/div/div[2]/div[2]/div[3]/div/div/div/a').click()
        sleep(7)
        # 查找 对应的订单编号
        # a = self.order_no
        # print(a)
        # bianhao = driver.find_element(By.LINK_TEXT, str(a)).text
        # print('后端-商品订单号', bianhao)
        # 点击详情
        # 点击退款
        driver.find_element(By.XPATH,'/html/body/div/div/section/div/form/div/div/div[1]/div[1]/div/a[3]').click()
        sleep(2)
        # 向下滑动
        driver.execute_script('window.scrollTo(0,300)')
        sleep(2)
        # 输入内容
        driver.find_element(By.NAME,'desc').send_keys('同意退款该商品')
        sleep(2)
        # 点击提交
        driver.find_element(By.XPATH,'/html/body/div/div/section/div/form/div/div[2]/div/button[1]').click()
        sleep(2)

        # 关闭浏览器驱动
        sleep(3)
        driver.quit()


if __name__ == '__main__':
    unittest.main()
